﻿package Interface.WelcomePageClasses
{	
	import com.junkbyte.console.Cc;
	
	import caurina.transitions.Tweener;
	
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.geom.Point;
	import flash.text.AntiAliasType;	
	import flash.display.BitmapData;
	import flash.display.Bitmap;	

	public class TextScroller extends MovieClip
	{
		private const CTweenTime:int = 1;
		private const CWidth:int = 270;
		private const CX:int = 35;
		
		private var FPagesCount:int;
		private var FCurrentPage:int;		
		private var FCanTween:Boolean;
		private var FText:Vector.<ScrollingLabel>;		
		private var FFakeFirst:Bitmap;
		private var FFakeFirstCanvas:BitmapData;		
		private var FFakeLast:Bitmap;
		private var FFakeLastCanvas:BitmapData;
		
		public function TextScroller() 
		{
			FText = new Vector.<ScrollingLabel>;
			//Bitmaps used for render first and last elements, provides infinite scroll effect
			FFakeLastCanvas = new BitmapData(CWidth, height, true, 0x00000000);			
			FFakeLast = new Bitmap();
			addChild(FFakeLast);			
			FFakeFirstCanvas = new BitmapData(CWidth, height, true, 0x00000000);
			FFakeFirst = new Bitmap();
			addChild(FFakeFirst);	
			//Removing static text
			removeChildAt(0);
			
			FCurrentPage = 0;
		}
		
		public function AddField(AText:String):ScrollingLabel
		{			
			var vText:ScrollingLabel = new ScrollingLabel(AText);			
			vText.x = FPagesCount * CWidth;
			FPagesCount++;
			addChild(vText);
			FText.push(vText);			
			UpdateBitmaps();			
			return vText;
		}
			
		private function UpdateBitmaps():void
		{
			FFakeLastCanvas = new BitmapData(CWidth, height, true, 0x00000000);
			FFakeLastCanvas.draw(FText[FText.length - 1]);
			FFakeLast.bitmapData = FFakeLastCanvas;
			FFakeLast.x = -CWidth;
			
			FFakeFirstCanvas = new BitmapData(CWidth, height, true, 0x00000000);
			FFakeFirstCanvas.draw(FText[0]);
			FFakeFirst.bitmapData = FFakeFirstCanvas;
			FFakeFirst.x = FPagesCount * CWidth;
		}
		
		public function get PagesCount():int
		{
			return FPagesCount;
		}
		
		public function MoveLeft():void
		{
			if(FCurrentPage > 0)			
				FCurrentPage--;			
			else
			{
				Page = FPagesCount;
				FCurrentPage--;				
			}
			
			Tweener.addTween(this, {x:GetX, time:CTweenTime, transition: "easeOutElastic"});
		}
		
		public function MoveRight():void
		{
			if(FCurrentPage < FPagesCount - 1)			
				FCurrentPage++;			
			else
			{				
				Page = -1;
				FCurrentPage++;						
			}
			
			Tweener.addTween(this, {x:GetX, time:CTweenTime, transition: "easeOutElastic"});
		}
		
		private function get GetX():Number
		{
			return CX - FCurrentPage * CWidth;
		}
		
		private function set Page(APage:int):void
		{
			x = CX - APage * CWidth;
			FCurrentPage = APage;
		}
	}	
}
